package com.jt.www.admin.order_pc.controller;

import com.jt.www.admin.order_pc.service.OrgMarketerService;
import com.jt.www.domain.po.OrgMarketer;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构营销员推广关系表
 * Created by shaoyn on 2020/07/28.
 */
@Api(value = "机构营销员推广关系", tags = {"ORG-MARKETER-API"}, description = "机构营销员关系")
@RestController
@RequestMapping(value = "/offline/order")
public class OrgMarketerController {

    @Autowired
    private OrgMarketerService orgMarketerService;

    public static final Logger log = LoggerFactory.getLogger(OrgMarketerController.class);

    /**
     * 添加推广关系
     *
     * @return
     */
    @ApiOperation(value = "添加推广关系", notes = "添加推广关系", httpMethod = "POST")
    @PostMapping(value = "/saveOrgMarketer", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity saveOrgMarketer(@RequestBody OrgMarketer orgMarketer) {

        if (StringUtils.isBlank(orgMarketer.getOrgUserCode()) || StringUtils.isBlank(orgMarketer.getMarketerUserCode())) {
            return GenericDataResponse.errorWithMsg("保存失败， 机构/营销员userCode不能为空!");
        }

        if (0 == orgMarketerService.save(orgMarketer)) {
            return GenericDataResponse.errorWithMsg("保存失败， 机构/营销员userCode不能为空!");
        }
        return GenericDataResponse.ok();
    }

    /**
     * 删除推广关系
     *
     * @return
     */
    @ApiOperation(value = "删除推广关系", notes = "删除推广关系", httpMethod = "POST")
    @PostMapping(value = "/deleteOrgMarketer", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity deleteOrgMarketer(@RequestBody OrgMarketer orgMarketer) {

        if (StringUtils.isBlank(orgMarketer.getOrgUserCode())) {
            return GenericDataResponse.errorWithMsg("删除失败， 机构userCode不能为空!");
        }
        int n = orgMarketerService.delete(orgMarketer);
        log.info("通过机构userCode: {}, 删除记录数为：{}", orgMarketer.getOrgUserCode(), n);
        return GenericDataResponse.ok();
    }


    /**
     * 查询机构推广人
     *
     * @return
     */
    @ApiOperation(value = "查询机构推广人", notes = "查询机构推广人", httpMethod = "GET")
    @GetMapping(value = "/queryOrgMarketer", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericDataResponse<OrgMarketer>> queryOrgMarketer(@ApiParam(value = "机构userCode", required = true)
                                                                             @RequestParam("orgUserCode") String orgUserCode) {

        if (StringUtils.isBlank(orgUserCode)) {
            return GenericDataResponse.errorWithMsg("机构userCode不能为空!", OrgMarketer.class);
        }
        List<OrgMarketer> orgMarketers = orgMarketerService.selectByOrgUserCodes(Lists.newArrayList(orgUserCode));
        if (CollectionUtils.isNotEmpty(orgMarketers)) {
            return GenericDataResponse.okWithData(orgMarketers.get(0));
        }

        return GenericDataResponse.okWithData(null);
    }

}
